package com.jiutong.meipengdai.ui.adapter;

import android.content.Context;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jiutong.meipengdai.R;
import com.jiutong.meipengdai.base.CommonAdapter;
import com.jiutong.meipengdai.base.ViewHolder;
import com.jiutong.meipengdai.entity.LoanDetailBean;
import com.jiutong.meipengdai.utils.NumUilt;

import java.util.List;

/**
 * 作用:
 * Created by 78 on 2017/6/2.
 */
public class LoanDetailAdapter extends CommonAdapter<LoanDetailBean.DataBean.RepayRecordsBean> {
    public String loanAmount;
    public String finalAmont;
    public String applyDayStaute;
    private List<LoanDetailBean.DataBean.LoanPostponesBean> loanPostpones;

    public LoanDetailAdapter(Context context, List<LoanDetailBean.DataBean.RepayRecordsBean> listDatas, int layoutId, String loanAmount, String amount, String applyDayStayte, List<LoanDetailBean.DataBean.LoanPostponesBean> loanPostpones) {
        super(context, listDatas, layoutId);
        this.loanAmount = loanAmount;
        this.finalAmont = amount;
        this.applyDayStaute = applyDayStayte;
        this.loanPostpones = loanPostpones;
    }

    @Override
    protected void fillData(ViewHolder holder, int position) {
        View loan_item_List_head = holder.getView(R.id.loan_item_List_head);
        View loan_detail_devider = holder.getView(R.id.loan_detail_devider);
        ImageView iv_loan_detail_list_mid = holder.getView(R.id.iv_loan_detail_list_mid);
        View loan_item_List_bottom = holder.getView(R.id.loan_item_List_bottom);
        TextView tv_loan_detail_time_list = holder.getView(R.id.tv_loan_detail_time_list);
        TextView tv_loan_detail_list_payment = holder.getView(R.id.tv_loan_detail_list_payment);
        TextView tv_loan_detail_list_interest = holder.getView(R.id.tv_loan_detail_list_interest);
        TextView tv_loan_detail_list_over = holder.getView(R.id.tv_loan_detail_list_over);
        TextView tv_loan_detail_init_money = holder.getView(R.id.tv_loan_detail_init_money);
        TextView tv_loan_detail_list_paymented = holder.getView(R.id.tv_loan_detail_list_paymented);
        TextView tv_apply_day_red = holder.getView(R.id.tv_apply_day_red);
        LinearLayout ll_apply_day_red = holder.getView(R.id.ll_apply_day_red);
        TextView tv_apply_day_nopass = holder.getView(R.id.tv_apply_day_nopass);
        TextView tv_apply_day_nopass_reason = holder.getView(R.id.tv_apply_day_nopass_reason);
        TextView tv_apply_day_gray = holder.getView(R.id.tv_apply_day_gray);
        TextView tv_apply_day_pass_green = holder.getView(R.id.tv_apply_day_pass_green);

        tv_loan_detail_time_list.setText(listDatas.get(position).getEstimatedRepaymentDate());
        tv_loan_detail_list_interest.setText(" ￥ " + listDatas.get(position).getLoanInterest());
        String remainDays = listDatas.get(position).getRemainDays();
        int rds = Integer.parseInt(remainDays);
        if (listDatas.get(position).getOverduePenalty().equals("null")) {    //判断是否有逾期罚款金额
            tv_loan_detail_list_over.setText("￥ 0.00  ");
        } else {
            tv_loan_detail_list_over.setText("￥ " + listDatas.get(position).getOverduePenalty() + "  ");

        }
        String totalPeriod = listDatas.get(position).getTotalPeriod();
        String currentPeriod = listDatas.get(position).getCurrentPeriod();

        if (getCount() > 1) {
            loan_item_List_bottom.setVisibility(View.VISIBLE);
            loan_detail_devider.setVisibility(View.VISIBLE);

            if (position > 0) {
                loan_item_List_head.setVisibility(View.VISIBLE);
            } else {
                loan_item_List_head.setVisibility(View.INVISIBLE);
            }
        }

        if (currentPeriod.equals("1") && !totalPeriod.equals("1")) {   //判断是否是第一期
            tv_loan_detail_init_money.setVisibility(View.GONE);
            iv_loan_detail_list_mid.setImageResource(R.mipmap.loan_detail_list_gray);
            tv_apply_day_red.setVisibility(View.GONE);
            tv_apply_day_gray.setVisibility(View.GONE);
            tv_apply_day_nopass.setVisibility(View.GONE);
            ll_apply_day_red.setVisibility(View.GONE);
            tv_apply_day_nopass_reason.setVisibility(View.GONE);
            tv_apply_day_pass_green.setVisibility(View.GONE);
            isOverDay(position, tv_loan_detail_list_payment, tv_loan_detail_list_paymented, rds, iv_loan_detail_list_mid);
        } else {
            if (currentPeriod.equals(totalPeriod)) {    //判断是否是最后一期  做展期的展示

                tv_loan_detail_init_money.setVisibility(View.VISIBLE);
                tv_loan_detail_init_money.setText("");
                isOverDay(position, tv_loan_detail_list_payment, tv_loan_detail_list_paymented, rds, iv_loan_detail_list_mid);
                tv_loan_detail_init_money.setText("本金￥ " + NumUilt.formatDouble(Double.parseDouble(loanAmount) / 10000) + " 万 +");
               } else {
                tv_loan_detail_init_money.setVisibility(View.GONE);
                iv_loan_detail_list_mid.setImageResource(R.mipmap.loan_detail_list_gray);
                isOverDay(position, tv_loan_detail_list_payment, tv_loan_detail_list_paymented, rds, iv_loan_detail_list_mid);
            }
        }

        checkApplyDday(listDatas.get(position), tv_apply_day_red, ll_apply_day_red, tv_apply_day_nopass, tv_apply_day_nopass_reason, tv_apply_day_gray, tv_apply_day_pass_green, loan_detail_devider);

        if (position == getCount() - 1) {
            loan_item_List_bottom.setVisibility(View.INVISIBLE);
            loan_detail_devider.setVisibility(View.INVISIBLE);
        }
    }

    /**
     * 检查展期状态
     * 审核状态0不显示，1申请中，2审核不通过，3审核通过
     *
     * @param tv_apply_day_red
     * @param ll_apply_day_red
     * @param tv_apply_day_nopass
     * @param tv_apply_day_nopass_reason
     */

    private void checkApplyDday(LoanDetailBean.DataBean.RepayRecordsBean dataBean, TextView tv_apply_day_red, LinearLayout ll_apply_day_red, TextView tv_apply_day_nopass, TextView tv_apply_day_nopass_reason,
                                TextView tv_apply_day_gray, TextView tv_apply_day_green, View loan_detail_devider) {
        if ("0".equals(dataBean.getPostponeResult())) {
//            tv_apply_day_red.setVisibility(View.VISIBLE);
//            tv_apply_day_red.setText(loanPostpones.get(0).getApplyTime() + "申请展期");
//            tv_apply_day_gray.setVisibility(View.GONE);
//            loan_detail_devider.setVisibility(View.VISIBLE);
//            tv_apply_day_green.setVisibility(View.GONE);
        } else if ("1".equals(dataBean.getPostponeResult())) {

            loan_detail_devider.setVisibility(View.INVISIBLE);
            tv_apply_day_red.setVisibility(View.VISIBLE);
            tv_apply_day_red.setText(dataBean.getLoanPostponePojo().getApplyTime() + "展期申请中");

        } else if ("3".equals(dataBean.getPostponeResult())) {
            tv_apply_day_gray.setVisibility(View.VISIBLE);
            tv_apply_day_green.setVisibility(View.VISIBLE);
            loan_detail_devider.setVisibility(View.INVISIBLE);
            tv_apply_day_gray.setText(dataBean.getLoanPostponePojo().getApplyTime() + "申请展期");
            tv_apply_day_green.setText(dataBean.getLoanPostponePojo().getAuditTime() + "展期申请通过");
        } else if ("2".equals(dataBean.getPostponeResult())) {
            tv_apply_day_red.setVisibility(View.VISIBLE);
            tv_apply_day_red.setBackground(mContext.getResources().getDrawable(R.drawable.shape_apply_day_gray));
            tv_apply_day_red.setText(dataBean.getLoanPostponePojo().getApplyTime() + "申请展期");
            tv_apply_day_red.setTextColor(mContext.getResources().getColor(R.color.gray));
            ll_apply_day_red.setVisibility(View.VISIBLE);
            tv_apply_day_nopass.setText(dataBean.getLoanPostponePojo().getAuditTime() + "展期申请未通过");
            tv_apply_day_nopass_reason.setText(dataBean.getLoanPostponePojo().getFailReason());

        }
    }

    /**
     * 是否逾期
     *
     * @param position
     * @param tv_loan_detail_list_payment
     * @param tv_loan_detail_list_paymented
     * @param rds
     * @param iv_loan_detail_list_mid
     */

    private void isOverDay(int position, TextView tv_loan_detail_list_payment, TextView tv_loan_detail_list_paymented, int rds, ImageView iv_loan_detail_list_mid) {
        if (rds <= 0) {     //剩余还款天数小于0  就是逾期
            if (listDatas.get(position).getRepayStatus().equals("2")) {
                tv_loan_detail_list_paymented.setText(listDatas.get(position).getRealRepaymentDate());
                tv_loan_detail_list_payment.setText("已还清");
                tv_loan_detail_list_payment.setTextColor(mContext.getResources().getColor(R.color.gray));
            } else {

                iv_loan_detail_list_mid.setImageResource(R.mipmap.loan_detail_list_red);
                tv_loan_detail_list_paymented.setText("已逾期");
                tv_loan_detail_list_payment.setText(rds * -1 + "天");
                tv_loan_detail_list_payment.setTextColor(mContext.getResources().getColor(R.color.red));
                tv_loan_detail_list_paymented.setTextColor(mContext.getResources().getColor(R.color.red));
            }

        } else {     //大于0就是正常
            setTitle(position, tv_loan_detail_list_payment, tv_loan_detail_list_paymented, rds, iv_loan_detail_list_mid);
        }
    }

    /**
     * 设置提示还款的标题
     *
     * @param position
     * @param tv_loan_detail_list_payment
     * @param tv_loan_detail_list_paymented
     * @param rds
     * @param iv_loan_detail_list_mid
     */
    private void setTitle(int position, TextView tv_loan_detail_list_payment, TextView tv_loan_detail_list_paymented, int rds, ImageView iv_loan_detail_list_mid) {
        if (listDatas.get(position).getRepayStatus().equals("1")) {    //判断贷款状态1为未还清
            if (Math.abs(rds) > 15) {         //剩余天数大于15天  设置为未还款
                tv_loan_detail_list_payment.setText("未还款");
                tv_loan_detail_list_payment.setTextColor(mContext.getResources().getColor(R.color.red));
                tv_loan_detail_list_paymented.setText("");
                iv_loan_detail_list_mid.setImageResource(R.mipmap.loan_detail_list_red);
            } else {                //小于15天设置为剩余天数
                iv_loan_detail_list_mid.setImageResource(R.mipmap.loan_detail_list_red);

                Spanned spanned = Html.fromHtml("剩余还款时间<font color='#ff0000'>" + listDatas.get(position).getRemainDays() + "</font>天");
                tv_loan_detail_list_payment.setText(spanned);
//                tv_loan_detail_list_payment.setText("剩余还款时间" + listDatas.get(position).getRemainDays() + "天");
                tv_loan_detail_list_paymented.setVisibility(View.INVISIBLE);
            }
        } else if (listDatas.get(position).getRepayStatus().equals("2")) {       //2是已还清
            tv_loan_detail_list_paymented.setText(listDatas.get(position).getRealRepaymentDate());
            tv_loan_detail_list_payment.setText("已还清");
        }
    }
}
